package com.weme.common.util

import android.content.Context
import android.os.Build
import android.provider.Settings
import android.text.TextUtils
import android.util.Log
import com.weme.common.BaseApplication
import java.net.NetworkInterface
import java.util.*

object DeviceId {
    private val HEX_DIGITS = charArrayOf('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F')
    private const val INVALID_MAC_ADDRESS = "02:00:00:00:00:00"
    private const val INVALID_ANDROID_ID = "9774d56d682e549c"
    private val macInArray: ByteArray?
        get() {
            try {
                val enumeration = NetworkInterface.getNetworkInterfaces() ?: return null
                while (enumeration.hasMoreElements()) {
                    val netInterface = enumeration.nextElement()
                    if (netInterface.name == "wlan0") {
                        return netInterface.hardwareAddress
                    }
                }
            } catch (e: Exception) {
                Log.e("tag", e.message, e)
            }
            return null
        }
    val longMac: Long
        get() {
            val bytes = macInArray
            if (bytes == null || bytes.size != 6) {
                return 0L
            }
            var mac = 0L
            for (i in 0..5) {
                mac = mac or (bytes[i].toInt() and 0xFF).toLong()
                if (i != 5) {
                    mac = mac shl 8
                }
            }
            return mac
        }
    val macAddress: String
        get() {
            val mac = formatMac(macInArray)
            return if (TextUtils.isEmpty(mac) || mac == INVALID_MAC_ADDRESS) {
                ""
            } else mac
        }

    private fun formatMac(bytes: ByteArray?): String {
        if (bytes == null || bytes.size != 6) {
            return ""
        }
        val mac = CharArray(17)
        var p = 0
        for (i in 0..5) {
            val b = bytes[i]
            mac[p] = HEX_DIGITS[b.toInt() and 0xF0 shr 4]
            mac[p + 1] = HEX_DIGITS[b.toInt() and 0xF]
            if (i != 5) {
                mac[p + 2] = ':'
                p += 3
            }
        }
        return String(mac)
    }

    private fun isAndroidIdValid(androidId: String): Boolean { // 是否有其他非法的AndroidId?
        return (!TextUtils.isEmpty(androidId) && androidId != "9774d56d682e549c" && androidId != "0000000000000000" && androidId != "0123456789abcdef")
    }

    fun getDeviceLanguage(): String {
        return Locale.getDefault().language
    }

    fun getDeviceCountry(): String {
        return Locale.getDefault().country
    }

    fun getUniqueDeviceId(context: Context?): String {
        val androidId = Settings.Secure.getString(context?.contentResolver, Settings.Secure.ANDROID_ID)
        val manufacturer = Build.MANUFACTURER
        val model = Build.MODEL

        val combinedId = "$androidId-$manufacturer-$model"

        // 进一步处理 combinedId，例如使用哈希函数
        // 以确保唯一性并返回最终的唯一标识符

        return combinedId
    }
    private var androidId: String = ""

    fun getAndroidId(): String {
        if (TextUtils.isEmpty(androidId)) {
            androidId = Settings.System.getString(BaseApplication.sInstance.contentResolver, Settings.Secure.ANDROID_ID)
        }
        return androidId ?: ""
    }


}